function waitPage() {
	var that;
	this.getParams = new getParams();
}

waitPage.prototype = {
	team_id:'',
	params:'',
	init_url:'../mobile/waitPageInfo',
	join_url:'../mobile/startGame',
	rechoose_url:'../mobile/reChoose',
	action:'',		// 1: 进入 2: 退出更换	
	isLock:false,
	open_id:'',
	meet_id:'',
	sequence:'',
    team:'',
    round:'',
    team_img:'',
    team_name:'',
	init:function() {
		that = this;
		that.params = window.location.search;
		that.open_id = that.getParams.getQueryString("openid");
		that.sequence = that.getParams.getQueryString("sequence");
		that.meet_id = that.getParams.getQueryString("meet_id");
        that.team = that.getParams.getQueryString("team");
		that._gameInit();
	},
	bindEvent:function() {
		$("#enter,#change").on("click",that._chooseAction);
	},
	// 游戏初始化
	_gameInit:function() {
		var data = {
				open_id:that.open_id,
				meet_id:that.meet_id,
				sequence:that.sequence,
			};
		that._request_data(that.init_url,data,that._gameInitCallback,that._gameInitErrorCallback);
	},
	_gameInitCallback:function(ret) {
		that.getParams.hideLoadMask();
		var data = ret;
		var teamInfo = ret.teamInfo;
			if(parseInt(data.reCode) == 1) {
				that.round = data.round;
				$("#pageBody").find("img").attr("src",teamInfo.team_image);
				$("#pageBody").find(".team-name").html(teamInfo.team_name);
				that.team_img = teamInfo.team_image;
				that.team_name = teamInfo.team_name;
				if(teamInfo.type == 2){
					$("#pageBody").find(".teamImg-group").addClass("blue");
				}
			}
			if(parseInt(data.reCode) == -1) {
				var txt = "游戏已经开始,请等待下一轮";
				that.getParams.showTips(txt);
			}
			//绑定事件
			that.bindEvent();
	},
	_gameInitErrorCallback:function(ret) {
		// window.location.reload();
	},
	_chooseAction:function() {
		if(that.isLock) {
			return;
		}
		that.action = $(this).attr("data-action");
		//进入游戏
		if(that.action == 1){
			that.startGame();
		}
		//退出更换
		if(that.action == 2){
			that.reChoose();
		}
	},
	//进入游戏
	startGame:function() {
		var data = {
				open_id:that.open_id,
				sequence:that.sequence,
				meet_id:that.meet_id,
			};
		that._request_data(that.join_url,data,that._startGameCallback,that._startGameErrorCallback);
	},
	_startGameCallback:function(ret) {
		var flag = parseInt(ret.res);
		// 被踢出
		if(flag == 0){
			window.location.href = "shotOff" + that.params;
		}
		// 进入游戏
		if(flag == 1){
			window.location.href = "shake" + that.params + "&round=" + that.round + "&team_img=" + that.team_img + "&team_name=" + that.team_name;
		}
		// 未开始
		if(flag == -1){
            var txt = "游戏未开始";
				that.getParams.showTips(txt);
		}
		//游戏结束
		if(flag == -2 ) {
			var txt = "游戏已经结束";
			that.getParams.showTips(txt);
			//window.location.href = "chooseSide" + that.params;
		}
        return false;
	},
	_startGameErrorCallback:function(ret) {

	},
	//退出更换
	reChoose:function() {
		var data = {
				open_id:that.open_id,
				sequence:that.sequence,
				meet_id:that.meet_id,
			};
		that._request_data(that.rechoose_url,data,that._reChooseCallback,that._reChooseErrorCallback);
	},
	_reChooseCallback:function(ret) {
		var flag = parseInt(ret.res);
		// 退出成功
		if(flag == 1){
			var index = window.location.search.indexOf('&team=');
			var params = window.location.search.substr(0,index);
			window.location.href = "chooseSide" + params;
		}
		if(flag == 2){
			var txt = "游戏已开始，不能退出，请进入游戏";
			that.getParams.showTips(txt);
		}
	},
	_reChooseErrorCallback:function(ret) {

	},
	_request_data:function(url,data,callback,errorCallback) {
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			data:data,
			success:function(ret) {
				callback && callback(ret);
			},
			error:function() {
				errorCallback && errorCallback();
			}
		})
	}
}

$(function() {
	var obj = new waitPage();
		obj.init();
})